cd "C:\RA\CA Disconnections\My Files"
use disconnections_final.dta, clear

*rate == customer_count/total households ... corr of 90%*
replace disconnections_rate = disconnections/total_households if utility=="LADWP"

*** years for which we have data from both IOU's and muni's -- 2018, 2019, 2020
drop if year < 2018
drop if utility == "SoCalGas"

**total degree days
gen tdd=cdd_tot + hdd_tot

**obs 1 ... (71.9409 + 51.0074)/2 - 65

**Clean**
drop id z
gen white_percent2=white_total/total_population*100
order white_percent2, after(white_percent)
drop white_percent
rename white_percent2 white_percent

gen black_percent2=black_total/total_population*100
order black_percent2, after(black_percent)
drop black_percent
rename black_percent2 black_percent

gen amindian_aknative_percent2=amindian_aknative_total/total_population*100
order amindian_aknative_percent2, after(amindian_aknative_percent)
drop amindian_aknative_percent
rename amindian_aknative_percent2 amindian_aknative_percent

gen asian_percent2=asian_total/total_population*100
order asian_percent2, after(asian_percent)
drop asian_percent
rename asian_percent2 asian_percent

gen other_percent2=other_total/total_population*100
order other_percent2, after(other_percent)
drop other_percent
rename other_percent2 other_percent

gen mixed_percent2=mixed_total/total_population*100
order mixed_percent2, after(mixed_percent)
drop mixed_percent
rename mixed_percent2 mixed_percent

gen under5_percent2=under5_total/total_population*100
order under5_percent2, after(under5_percent)
drop under5_percent
rename under5_percent2 under5_percent

gen hispanic_percent2=hispanic_total/total_population*100
order hispanic_percent2, after(hispanic_percent)
drop hispanic_percent
rename hispanic_percent2 hispanic_percent

gen renter_percent = renter_occupied/occupied_units*100
gen owner_percent = owner_occupied/occupied_units*100

gen muni=1 if utility=="LADWP"
gen iou=1 if utility=="PGE" | utility=="SCE" | utility=="SDGE"

*drop housing_1detached housing_1attached housing_2apts housing_34apts housing_59apts housing_10apts housing_mobile

**generating region based on CA census: https://census.ca.gov/regions/
*1=LA, 2=Orange, 3=Ventura, 4= San Bernardino, 5=Riverside, 6=San Diego, 7=Imperial, 8=Inyo, 9=Tulare, 10-Kings, 11=Kern, 12=Fresno, 13=Santa Barbara, 14=San Luis Obispo, 15=Monterey, 16=Mono, 17=Madera, 18=Merced, 19=Mariposa, 20=San Mateo, 21=Santa Clara, 22=San Fransisco, 23=Sacramento, 24=Alameda, 25=Napa, 26=Contra Costa, 27=Solano, 28=Marin, 29=Sonoma, 30=Santa Cruz, 31=San Benito, 32=San Joaquin, 33=Calaveras, 34=Tuolumne, 35=Mariposa, 36=Stanislaus, 37=Mendocino, 38=Lake, 39=Humboldt, 40=Trinity, 41=Del Norte, 42=Siskiyou, 43=Amador, 44=Placer, 45=Yolo, 46=El Dorado, 47=Alpine, 48=Nevada, 49=Sierra, 50=Colusa, 51=Glenn, 52=Butte, 53=Plumas, 54=Yuba, 55=Sutter, 56=Shasta, 57=Modoc, 58=Lassen, 59=Tehama, 60=Lassen
gen county=.
replace county = 1 if zip < 90620 | zip > 90633 & zip < 90671 | zip > 90680 & zip < 09717 | zip > 90721 & zip < 90740 | zip > 90743 & zip < 91316 | zip > 91320 & zip < 91358 | zip > 91362 & zip < 91377 | zip > 91377 & zip < 91701 | zip > 91701 & zip < 91708 | zip > 91710 & zip < 91729 | zip > 91730 & zip < 91737 | zip > 91739 & zip < 91743 | zip > 91743 & zip < 91752 | zip > 91752 & zip < 91758 | zip == 91759 | zip > 91765 & zip < 91784 | zip > 91786 & zip < 91798 | zip > 91798 & zip < 91901 | zip==93501 | zip > 93531 & zip < 93541 | zip==93543 | zip == 93544 | zip > 93549 & zip < 93544 | zip==93563 | zip < 93581 & zip < 93592 | zip==93599
replace county = 2 if zip > 90612 & zip < 90637 | zip == 90680 | zip==90720 | zip==90721 | zip > 90734 & zip < 90744 | zip > 92599 & zip < 92860 | zip > 92680 & zip < 92877 | zip > 92883 & zip < 93001
replace county = 3 if zip==91319 | zip==91320 | zip > 91357 & zip < 91363 | zip == 91377 | zip > 92899 & zip < 93013 | zip > 93014 & zip < 93067 | zip > 93067 & zip < 93101
replace county = 4 if zip==91701 | zip > 91706 & zip < 91711 | zip==91729 | zip==91730 | zip==91737 | zip==91739 | zip==91743 | zip==91758 | zip > 91759 & zip < 91765 | zip > 91780 & zip < 91788 | zip==91798 | zip==92242 | zip==92252 | zip==92256 | zip ==92267 | zip==92268 | zip > 92276 & zip < 92281 | zip > 92283 & zip < 92292 | zip > 92292 & zip < 92320 | zip > 92320 & zip < 92328 | zip > 92328 & zip < 92384 | zip > 92384 & zip < 92389 | zip > 92389 & zip < 92501 | zip==93558 | zip==93562 | zip==93592
replace county = 5 if zip==91752 | zip > 92199 & zip < 92222 | zip > 92222 & zip < 92227 | zip==92230 | zip > 92233 & zip < 92242 | zip==92247 | zip==92248 | zip > 92252 & zip < 92256 | zip==92258 | zip > 92259 & zip < 92266 | zip==92274 | zip==92276 | zip==92282 | zip==92292 | zip==92320 | zip > 92427 & zip < 92602 | zip==92860 | zip > 92871 & zip < 92885
replace county = 6 if zip > 91899 & zip < 92201
replace county = 7 if zip==92222 | zip==92227 | zip > 92230 & zip < 92234 | zip==92243 | zip==92244 | zip > 92248 & zip < 92252 | zip==92257 | zip==92259 | zip==92266 | zip==92273 | zip==92275 | zip==92281 | zip==92283
replace county = 8 if zip==92328 | zip==92384 | zip==92389 | zip > 93512 & zip < 93516 | zip==93522 | zip==93526 | zip==93530 | zip==93542 | zip==93545 | zip==93549
replace county = 9  if zip==93201 | zip==93207 | zip==93208 | zip==93218 | zip==93219 | zip==93221 | zip==93223 | zip==93227 | zip==93235 | zip==93237 | zip==93244 | zip==93247 | zip > 93255 & zip < 93263 | zip==93265 | zip==93267 | zip > 93268 & zip < 93276 | zip > 93276 & zip < 93280 | zip==93282 | zip==93286 | zip > 93287 & zip < 93301 | zip==93603 | zip==93615 | zip==93618 | zip==93633 | zip==93647 | zip==93666 | zip==93670 | zip==93673
replace county = 10 if zip==93202 | zip==93204 | zip==93212 | zip==93230 | zip==93232 | zip==93239 | zip==93245 | zip==93246 | zip==93266
replace county = 11 if zip==93203 | zip==93205 | zip==93206 | zip==93215 | zip==93216 | zip==93220 | zip==93222 | zip > 93223 & zip < 93227 | zip==93238 | zip==93240 | zip==93241 | zip==93243 | zip > 93247 & zip < 93254 | zip==93255 | zip==93263 | zip==93268 | zip==93276 | zip==93280 | zip==93283 | zip==93285 | zip==93287 | zip > 93292 & zip < 93401 | zip > 93483 & zip < 93510 | zip==93516 | zip==93518 | zip==93519 | zip==93523 | zip==93524 | zip==93527 | zip==93528 | zip==93531 | zip > 93553 & zip < 93558 | zip==93560 | zip==93561 | zip==93581 | zip==93596
replace county = 12 if zip==93210 | zip==93234 | zip==93242 | zip==93602 | zip > 93604 & zip < 93610 | zip > 93610 & zip < 93614 | zip==93616 | zip==93619 | zip==93621 | zip==93622 | zip > 93623 & zip < 93633 | zip==93634 | zip > 93639 & zip < 93643 | zip==93646 | zip > 93647 & zip < 93653 | zip > 93653 & zip < 93661 | zip==93662 | zip==93664 | zip==93667 | zip==93668 | zip > 93673 & zip < 93901
replace county = 13 if zip==93013 | zip==93014 | zip==93067 | zip > 93099 & zip < 93201 | zip==93254 | zip==93427 | zip==93429 | zip==93434 | zip > 93435 & zip < 93442 | zip > 93453 & zip < 93461 | zip==93463 | zip==93464
replace county = 14 if zip > 93390 & zip < 93426 | zip==93438 | zip > 93429 & zip < 93434 | zip==93435 | zip > 93441 & zip < 93450 | zip > 93450 & zip < 93454 | zip==93461 | zip > 93464 & zip < 93501
replace county = 15 if zip==93426 | zip==93450 | zip > 93888 & zip < 94002 | zip==95004 | zip==95012 | zip==95039
replace county = 16 if zip==93512 | zip==93517 | zip==93529 | zip==93541 | zip==93546 | zip==96107 | zip==96133
replace county = 17 if zip==93601 | zip==93604 | zip==93610 | zip==93614 | zip > 93635 & zip < 93640 | zip > 93642 & zip < 93646 | zip==93653 | zip==93669
replace county = 18 if zip==93620 | zip==93635 | zip==93661 | zip==93665 | zip==93501 | zip==93503 | zip==95312 | zip==93515 | zip==93522 | zip==93524 | zip==95333 | zip==93534 | zip > 95338 & zip < 95345 | zip==95348 | zip==95365 | zip==95369 | zip==95374 | zip==95388
replace county = 19 if zip==93623 | zip==95306 | zip==93511 | zip==95318 | zip==95325 | zip==95338 | zip==95345 | zip==95389
replace county = 20 if zip > 93962 & zip < 94022 | zip > 94024 & zip < 94035 | zip==94037 | zip==94038 | zip > 94043 & zip < 94083 | zip==94096 | zip==94098 | zip==94128 | zip > 94309 & zip < 94501
replace county = 21 if zip > 94021 & zip < 94028 | zip==94035 | zip > 94038 & zip < 94044 | zip > 94083 & zip < 94096 | zip > 94288 & zip < 94401 | zip==95002 | zip==95008 | zip==95009 | zip==95011 | zip > 95012 & zip < 95017 | zip==95020 | zip==95021 | zip > 95024 & zip < 95033 | zip > 95033 & zip < 95039 | zip==95042 | zip > 95045 & zip < 95060 | zip==95070 | zip==95071 | zip > 95077 & zip < 95201
replace county = 22 if zip > 94098 & zip < 94128 | zip > 94128 & zip < 94203
replace county = 23 if zip > 94199 & zip < 94301 | zip > 95607 & zip < 95612 | zip==95615 | zip==95621 | zip==95624 | zip==95626 | zip==95628 | zip==95630 | zip==95632 | zip==95638 | zip==95639 | zip==95641 | zip==95652 | zip==95655 | zip==95660 | zip==95662 | zip==95670 | zip==95671 | zip==95673 | zip==95680 | zip==95683 | zip==95690 | zip==95693 | zip==95741 | zip==95742 | zip > 95747 & zip < 95762 | zip==95763 | zip > 95799 & zip < 95901
replace county = 24 if zip==94501 | zip==94502 | zip > 94535 & zip < 94547 | zip > 94549 & zip < 94553 | zip==94555 | zip==94557 | zip==94560 | zip==94566 | zip==94568 | zip > 94576 & zip < 94581 | zip > 94585 & zip < 94589 | zip > 94599 & zip < 94801
replace county = 25 if zip==94503 | zip==94508 | zip==94515 | zip==94558 | zip==94559 | zip==94562 | zip==94567 | zip==94573 | zip==94574 | zip==94576 | zip==94581 | zip==94599
replace county = 26 if zip==94506 | zip==94507 | zip==94509 | zip==94511 | zip==94513 | zip==94514 | zip > 94515 & zip < 94533 | zip > 94546 & zip < 94550 | zip==94553 | zip==94556 | zip==94561 | zip > 94562 & zip < 94566 | zip==94569 | zip==94570 | zip==94572 | zip==94575 | zip==94582 | zip==94583 | zip > 94592 & zip < 94599 | zip > 94720 & zip < 94901
replace county = 27 if zip==94510 | zip==94512 | zip > 94531 & zip < 94536 | zip==94571 | zip==94585 | zip > 94588 & zip < 94595 | zip==95620 | zip==95625 | zip==95687 | zip==95688 | zip==95696
replace county = 28 if zip > 94850 & zip < 94922 | zip==94924 | zip==94925 | zip==94929 | zip==94930 | zip > 94931 & zip < 94951 | zip > 94955 & zip < 94972 | zip==94973 | zip==94974 | zip > 94975 & zip < 94999
replace county = 29 if zip==94922 & zip==94923 | zip > 94925 & zip < 94929 | zip==94931 | zip > 94950 & zip < 94956 | zip==94972 | zip==94975 | zip==94999 | zip > 95397 & zip < 95410 | zip==95412 | zip==95416 | zip==95419 | zip==95421 | zip==95425 | zip==95430 | zip==95431 | zip==95433 | zip==95436 | zip > 95437 & zip < 95443 | zip==95444 | zip==95446 | zip==95448 | zip==95450 | zip==95452 | zip==95462 | zip==95465 | zip > 95470 & zip < 95481 | zip==95486 | zip==95487 | zip==95497
replace county = 30 if zip==95001 | zip==95003 | zip > 95004 & zip < 95008 | zip==95010 | zip > 95015 & zip < 95020 | zip==95033 | zip==95041 | zip > 95056 & zip < 95070 | zip==95073 | zip==95076 | zip==95077
replace county = 31 if zip==95023 | zip==95024 | zip==95043 | zip==95045 | zip==95075
replace county = 32 if zip > 95196 & zip < 95221 | zip==95230 | zip==95231 | zip > 95233 & zip < 95245 | zip==95253 | zip > 95257 & zip < 95301 | zip==95304 | zip==95320 | zip==95330 | zip==95336 | zip==95337 | zip==95366 | zip > 95375 & zip < 95379 | zip==95385 | zip==95391 | zip==95686
replace county = 33 if zip > 95220 & zip < 95227 | zip==95228 | zip==95229 | zip==95232 | zip==95233 | zip > 95242 & zip < 95253 | zip > 95253 & zip < 95258
replace county = 34 if zip==95305 | zip==95309 | zip==95310 | zip==95314 | zip==95321 | zip==95327 | zip==95335 | zip==95346 | zip==95347 | zip==95364 | zip > 95369 & zip < 95374 | zip==95375 | zip==95379 | zip==95383
replace county = 35 if zip==93623 | zip==95306 | zip==95311 | zip==95318 | zip==95325 | zip==95338 | zip==95345 | zip==95389
replace county = 36 if zip==95307 | zip==95313 | zip==95316 | zip==95319 | zip==95323 | zip==95326 | zip==95328 | zip==95329 | zip > 95348 & zip < 95364 | zip==95367 | zip==95368 | zip > 95379 & zip < 95383 | zip==95386 | zip==95387 | zip==95397
replace county = 37 if zip==95410 | zip==95415 | zip==95417 | zip==95418 | zip==95420 | zip > 95426 & zip < 95430 | zip==95432 | zip==95437 | zip==95445 | zip==95449 | zip==95454 | zip==95456 | zip==95459 | zip==95460 | zip==95466 | zip > 95467 & zip < 95471 | zip==95481 | zip==95482 | zip==95488 | zip==95490 | zip==95494 | zip==95585 | zip==95587
replace county = 38 if zip > 95421 & zip < 95425 | zip==95426 | zip==95435 | zip==95443 | zip==95451 | zip==95453 | zip==95457 | zip==95458 | zip==95461 | zip==95464 | zip==95467 | zip==95485 | zip==95493
replace county = 39 if zip > 95497 & zip < 95527 | zip==95528 | zip > 95532 & zip < 95538 | zip==95540 | zip==95542 | zip > 95543 & zip < 95548 | zip > 95548 & zip < 95552 | zip > 95552 & zip < 95563 | zip==95564 | zip==95565 | zip > 95568 & zip < 85585 | zip==95589
replace county = 40 if zip==95527 | zip==95552 | zip==95563 | zip==95595 | zip==96010 | zip==96024 | zip==96041 | zip==96046 | zip==96048 | zip==96052 | zip==96091 | zip==96093
replace county = 41 if zip==95531 | zip==95532 | zip==95538 | zip==95543 | zip==95548 | zip==95567
replace county = 42 if zip==95568 | zip==96014 | zip==96023 | zip==96025 | zip==96027 | zip==96031 | zip==96032 | zip==96034 | zip > 96035 & zip < 96040 | zip==96044 | zip==96050 | zip==96057 | zip==96058 | zip==96064 | zip==96067 | zip==96085 | zip==96086 | zip==96094 | zip==96097 | zip==96134
replace county = 43 if zip==95601 | zip==95629 | zip==95640 | zip==95642 | zip==95644 | zip==95654 | zip==95665 | zip==95666 | zip==95669 | zip==95675 | zip==95685 | zip==95689 | zip==95699
replace county = 44 if zip > 95601 & zip < 95605 | zip==95631 | zip==95648 | zip==95650 | zip==95658 | zip==95661 | zip==95663 | zip==95677 | zip==95678 | zip==95681 | zip==95701 | zip==95703 | zip > 95712 & zip < 95710 | zip==95722 | zip==95736 | zip==95746 | zip==95747 | zip==95765 | zip==96140 | zip==96141 | zip > 96142 & zip < 96150
replace county = 45 if zip > 95604 & zip < 95608 | zip==95612 | zip > 95615 & zip < 95619 | zip==95627 | zip==95637 | zip==95645 | zip==95653 | zip==95679 | zip==95691 | zip==95694 | zip==95695 | zip==95697 | zip==95698 | zip > 95765 & zip < 95812 | zip==95937
replace county = 46 if zip==95613 | zip==95614 | zip==95619 | zip==95623 | zip > 95632 & zip < 95637 | zip==95651 | zip==95656 | zip==95664 | zip==95667 | zip==95672 | zip==95682 | zip==95684 | zip==95709 | zip==95720 | zip==95721 | zip==95726 | zip==95735 | zip==95762 | zip==96142 | zip > 96148 & zip < 96160
replace county = 47 if zip==95646 | zip==96120
replace county = 48 if zip==95712 | zip==95724 | zip==95728 | zip==95924 | zip==95945 | zip==95946 | zip==95949 | zip==95959 | zip==95960 | zip==95975 | zip==95977 | zip==95986 | zip==96111 | zip > 96158
replace county = 49 if zip==95910 | zip==95936 | zip==95944 | zip==96118 | zip > 96123 & zip < 96127
replace county = 50 if zip==95912 | zip==95932 | zip==95950 | zip==95955 | zip==95970 | zip==95979 | zip==95987
replace county = 51 if zip==95913 | zip==95920 | zip==95939 | zip==95943 | zip==95951 | zip==95963 | zip==95988
replace county = 52 if zip==95914 | zip==95916 | zip==95917 | zip > 95925 & zip < 95932 | zip==95938 | zip > 95939 & zip < 95943 | zip==95948 | zip==95954 | zip==95958 | zip > 95963 & zip < 95970 | zip==95973 | zip==95974 | zip==95976 | zip==95978
replace county = 53 if zip==95915 | zip==95923 | zip==95934 | zip==95947 | zip==95956 | zip==95971 | zip==95980 | zip==95983 | zip==95984 | zip==96020 | zip==96103 | zip==96105 | zip==96106 | zip==96122 | zip==96129 | zip==96135
replace county = 54 if zip==95692 | zip==95901 | zip==95903 | zip==95918 | zip==95919 | zip==95922 | zip==95925 | zip==95935 | zip==95961 | zip==95932 | zip==95972 | zip==95981
replace county = 55 if zip==95659 | zip==95668 | zip==95674 | zip==95676 | zip==95953 | zip==95957 | zip==95982 | zip > 95988 & zip < 96001
replace county = 56 if zip > 95993 & zip < 96006 | zip==96007 | zip==96008 | zip==96011 | zip==96013 | zip > 96015 & zip < 96020 | zip==96022 | zip==96028 | zip==96033 | zip==96040 | zip==96047 | zip==96049 | zip==96051 | zip==96062 | zip > 96068 & zip < 96074 | zip==96076 | zip==96079 | zip==96084 | zip > 96086 & zip < 96090 | zip==96095 | zip==96096 | zip==96099
replace county = 57 if zip==96006 | zip==96015 | zip==96054 | zip==96101 | zip==96104 | zip==96108 | zip==96110 | zip==96112 | zip==96115 | zip==96116
replace county = 58 if zip==96009 | zip==96056 | zip==96068 | zip==96109 | zip==96113 | zip==96114 | zip==96117 | zip==96119 | zip==96121 | zip==96123 | zip==96127 | zip==96128 | zip==96130 | zip==96132 | zip==96136 | zip==96137
replace county = 59 if zip==96021 | zip==96029 | zip==96035 | zip==96055 | zip==96059 | zip==69061 | zip==96063 | zip==96074 | zip==96075 | zip==96078 | zip==96080 | zip==96090 | zip==96092
replace county = 60 if zip==96009 | zip==96056 | zip==96068 | zip==96109 | zip==96113 | zip==96114 | zip==96117 | zip==96119 | zip==96121 | zip==96123 | zip==96127 | zip==96128 | zip==96130 | zip==96132 | zip==96136 | zip==96137

gen region=.
replace region = 1 if county==52 | county==50 | county==51 | county==59 | county==58 | county==48 | county==44 | county==53 | county==23 | county==57 | county==49 | county==42 | county==56 | county==60 | county==45 | county==54
replace region = 2 if county==41 | county==39 | county==38 | county==37 | county==25 | county==29 | county==40
replace region = 3 if county==24 | county==26 | county==28 | county==22 | county==20 | county==21 | county==27
replace region = 4 if county==47 | county==33 | county==17 | county==19 | county==18 | county==16 | county==32 | county==36 | county==34
replace region = 5 if county==15 | county==31 | county==14 | county==13 | county==30 | county==3
replace region = 6 if county==12 | county==8 | county==11 | county==10 | county==9
replace region = 7 if county==5 | county==4
replace region = 8 if county==1
replace region = 9 if county==2
replace region = 10 if county==8 | county==6

xi i.month
encode utility, gen(utility2)
xi i.utility2
xi i.year
xi i.region

**median_income categorical for visual
gen median_category = 1 if median_income < 20000
replace median_category = 2 if median_income > 20000 & median_income < 30000
replace median_category = 3 if median_income > 30000 & median_income < 40000
replace median_category = 4 if median_income > 40000 & median_income < 50000
replace median_category = 5 if median_income > 50000 & median_income < 60000
replace median_category = 6 if median_income > 60000 & median_income < 70000
replace median_category = 7 if median_income > 70000 & median_income < 80000
replace median_category = 8 if median_income > 80000 & median_income < 90000
replace median_category = 9 if median_income > 90000 & median_income < 100000
replace median_category = 10 if median_income > 100000 & median_income < 120000
replace median_category = 11 if median_income > 120000 & median_income < 140000
replace median_category = 12 if median_income > 140000 & median_income < 160000
replace median_category = 13 if median_income > 160000 & median_income < 180000
replace median_category = 14 if median_income > 180000 & median_income < 200000
replace median_category = 15 if median_income > 200000

*temperature lag*
sort month
bysort month: gen temp_lag=tmean_mean[_n-1]

sort month
bysort month: gen temp_lag2=tmean_mean[_n-2]

gen month_utility = month*utility2
xi i.month_utility

gen energy_burden2 = encost / median_income * 100
gen income2 = median_income/1000
gen costs2 = encost/1000
gen hh2 = total_households/1000
gen disconnections_ratex1000 = disconnections_rate * 1000

replace protected_days =0 if muni==1


*******
label var year "Year"
label var month "Month"
set scheme s1color

***Disconnection Graphs
gen scedison_disconnections = .
replace scedison_disconnections = 0 if utility2 ==3 & disconnections==0
replace scedison_disconnections = disconnections if utility2==3

gen pge_disconnections = .
replace pge_disconnections = 0 if utility2 ==2 & disconnections==0
replace pge_disconnections = disconnections if utility2==2 

gen sdge_disconnections = .
replace sdge_disconnections =0 if utility2==4 & disconnections==0
replace sdge_disconnections = disconnections if utility2==4

gen ladwp_disconnections = .
replace ladwp_disconnections =0 if utility2==1 & disconnections==0
replace ladwp_disconnections = disconnections if utility2==1

bysort year: egen sc_ed = mean(scedison_disconnections) if scedison_disconnections !=.
bysort year: egen pge = mean(pge_disconnections) if pge_disconnections !=.
bysort year: egen sdge = mean(sdge_disconnections) if sdge_disconnections !=.
bysort year: egen ladwp = mean(ladwp_disconnections) if ladwp_disconnections !=.


label var sc_ed "Southern California Edison"
label var pge "Pacific Gas & Electric"
label var sdge "San Diego Gas & Electric"
label var ladwp "Los Angeles Department of Water and Power"

sort year
graph twoway connected sc_ed pge sdge ladwp year, msymbol(T S D X) ylabel(0(25)125) ///
    ytitle("Mean Number of Monthly Disconnections") tlabel(2018(1)2020) ///
    legend(label(1 "Southern California Edison") label(2 "Pacific Gas & Electric") ///
    label(3 "San Diego Gas & Electric") label(4 "Los Angeles Department of Water and Power") col(1)) ///
    graphregion(color(white)) subtitle(" ", size(5)) ///
    xlabel(2018(1)2020, labsize(5)) ylabel(, labsize(5)) ytitle("Mean Number of Disconnections", size(5)) ///
    xtitle("Year", size(5)) legend(size(5)) xsize(8.8) ysize(13)
	

gr save graph1, replace

bysort month: egen sc_ed_m = mean(scedison_disconnections)
bysort month: egen pge_m = mean(pge_disconnections)
bysort month: egen sdge_m = mean(sdge_disconnections)
bysort month: egen ladwp_m = mean(ladwp_disconnections)

sort month
graph twoway connected sc_ed_m pge_m sdge_m ladwp_m month, msymbol(T S D X) ylabel(0(25)125) ///
    tlabel(1(1)12) legend(label(1 "Southern California Edison") label(2 "Pacific Gas & Electric") ///
    label(3 "San Diego Gas & Electric") label(4 "Los Angeles Department of Water and Power") col(1)) ///
    graphregion(color(white)) subtitle(" ", size(5)) ///
    xlabel(1(1)12, labsize(5)) ylabel(, labsize(5)) ///
    xtitle("Month") legend(size(5)) xsize(8.8) ysize(13)
gr save graph2, replace

grc1leg graph1.gph graph2.gph
gr export Figure1.png, replace


bysort tmean_mean: egen temp_dis = mean(disconnections)
label var tmean_mean "Mean Monthly Temperature (°C)"
label var temp_dis "Disconnections"

set scheme s1color

**MAPPING WEATHER BY UTILITY**
graph twoway connected temp_dis tmean_mean if utility2 == 1, title("Los Angeles Department of Water and Power", size(4.5)) ///
    xlabel(-5(10)35, labsize(4.5)) ylabel(, labsize(4.5)) xtitle("Mean Monthly Temperature", size(4.5)) ///
    ytitle("Disconnections" " ", size(4.5)) legend(size(4.5)) graphregion(color(white)) ///
    xsize(8.8) ysize(13) 
gr save ladwp, replace
gr export ladwp.png, replace

graph twoway connected temp_dis tmean_mean if utility2 == 2, title("Pacific Gas & Electric", size(4.5)) ///
    xlabel(-5(10)35, labsize(4.5)) ylabel(, labsize(4.5)) xtitle("Mean Monthly Temperature", size(4.5)) ///
    ytitle("Disconnections", size(4.5)) legend(size(4.5)) graphregion(color(white)) ///
    xsize(8.8) ysize(13)
gr save pge, replace
gr export pge.png, replace

graph twoway connected temp_dis tmean_mean if utility2 == 3, title("Southern California Edison", size(4.5)) ///
    xlabel(-5(10)35, labsize(4.5)) ylabel(, labsize(4.5)) xtitle("Mean Monthly Temperature", size(4.5)) ///
    ytitle("Disconnections", size(4.5)) legend(size(4.5)) graphregion(color(white)) ///
    xsize(8.8) ysize(13)
gr save sced, replace
gr export sced.png, replace

graph twoway connected temp_dis tmean_mean if utility2 == 4, title("San Diego Gas & Electric", size(4.5)) ///
    xlabel(-5(10)35, labsize(4.5)) ylabel(, labsize(4.5)) xtitle("Mean Monthly Temperature", size(4.5)) ///
    ytitle("Disconnections", size(4.5)) legend(size(4.5)) graphregion(color(white)) ///
    xsize(8.8) ysize(13)
gr save sdge, replace
gr export sdge.png, replace


gr combine ladwp.gph pge.gph sced.gph sdge.gph, ysize(13) xsize(21.2)
gr export FigureS1.png, replace




****Regresions*****
global discontrols black_percent hispanic_percent under5_percent over65_percent income2 renter_percent hh2 costs2 tmean_mean protected_days
global ratecontrols black_percent hispanic_percent under5_percent over65_percent income2 renter_percent costs2 tmean_mean protected_days

global fe i.month i.year i.utility2

***TABLE S1****
reg disconnections $discontrols $fe month_utility housing_*
outreg2 using TableS1.rtf, keep($discontrols i.utility2) stats(coef se) alpha(0.01, 0.05, 0.1) dec(3) pdec(3) replace
reg disconnections_ratex1000 $ratecontrols $fe month_utility housing_*
outreg2 using TableS1.rtf, keep($ratecontrols i.utility2) stats(coef se) alpha(0.01, 0.05, 0.1) dec(3) pdec(3) append

****TABLE 2*****
reg disconnections $discontrols $fe housing_*
outreg2 using Table2.rtf, keep($discontrols i.utility2 housing_*) stats(coef se) alpha(0.01, 0.05, 0.1) dec(3) pdec(3) replace
reg disconnections_ratex1000 $ratecontrols $fe housing_*
outreg2 using Table2.rtf, keep($ratecontrols i.utility2 housing_*) stats(coef se) alpha(0.01, 0.05, 0.1) dec(3) pdec(3) append

**TABLE S2***
global fe i.month i.year i.utility2 i.county
reg disconnections $discontrols $fe housing_*
outreg2 using TableS2.rtf, keep($discontrols i.utility2) stats(coef se) alpha(0.01, 0.05, 0.1) dec(3) pdec(3) replace
reg disconnections_ratex1000 $ratecontrols $fe housing_*
outreg2 using TableS2.rtf, keep($ratecontrols i.utility2) stats(coef se) alpha(0.01, 0.05, 0.1) dec(3) pdec(3)

***TABLE S3***
global fe i.month i.year i.utility2
reg disconnections c.black_percent##c.income2 c.hispanic_percent##c.income2 under5_percent over65_percent renter_percent hh2 costs2 tmean_mean protected_days $fe housing_*
outreg2 using TableS3.rtf, keep(c.black_percent##c.income2 c.hispanic_percent##c.income2 under5_percent over65_percent renter_percent hh2 costs2 tmean_mean protected_days i.utility2) stats(coef se) alpha(0.01, 0.05, 0.1) dec(3) pdec(3) replace
reg disconnections_ratex1000 c.black_percent##c.income2 c.hispanic_percent##c.income2 under5_percent over65_percent renter_percent costs2 tmean_mean protected_days $fe housing_*
outreg2 using TableS3.rtf, keep(c.black_percent##c.income2 c.hispanic_percent##c.income2 under5_percent over65_percent renter_percent costs2 tmean_mean protected_days i.utility2) stats(coef se) alpha(0.01, 0.05, 0.1) dec(3) pdec(3) append



****TABLE S4*****
reg disconnections c.black_percent##c.familyhome_percent c.hispanic_percent##c.familyhome_percent c.black_percent##c.renter_percent c.hispanic_percent##c.renter_percent income2 under5_percent over65_percent renter_percent hh2 tmean_mean protected_days $fe housing_2apts housing_34apts housing_59apts housing_10apts housing_mobile housing_2014 housing_2010_2013 housing_2000_2009 housing_1980_1999 housing_1960_1979 housing_1940_1959 housing_1939 housing_rooms_1 housing_rooms_23 housing_rooms_45 housing_rooms_67 housing_rooms_8
outreg2 using TableS4.rtf, keep(c.black_percent##c.familyhome_percent c.hispanic_percent##c.familyhome_percent c.black_percent##c.renter_percent c.hispanic_percent##c.renter_percent income2 under5_percent over65_percent renter_percent hh2 tmean_mean protected_days i.utility2) stats(coef se) alpha(0.01, 0.05, 0.1) dec(3) pdec(3) replace

reg disconnections_ratex1000 c.black_percent##c.familyhome_percent c.hispanic_percent##c.familyhome_percent c.black_percent##c.renter_percent c.hispanic_percent##c.renter_percent income2 under5_percent over65_percent renter_percent tmean_mean protected_days $fe housing_2apts housing_34apts housing_59apts housing_10apts housing_mobile housing_2014 housing_2010_2013 housing_2000_2009 housing_1980_1999 housing_1960_1979 housing_1940_1959 housing_1939 housing_rooms_1 housing_rooms_23 housing_rooms_45 housing_rooms_67 housing_rooms_8
outreg2 using TableS4.rtf, keep(c.black_percent##c.familyhome_percent c.hispanic_percent##c.familyhome_percent c.black_percent##c.renter_percent c.hispanic_percent##c.renter_percent income2 under5_percent over65_percent renter_percent tmean_mean protected_days i.utility2) stats(coef se) alpha(0.01, 0.05, 0.1) dec(3) pdec(3) append


***TABLE S5***
gen majority_minority = 1 if white_percent < 50
reg disconnections $discontrols $fe housing_* if majority_minority==1
outreg2 using TableS5.rtf, keep($discontrols i.utility2 housing_*)  stats(coef se) alpha(0.01, 0.05, 0.1) dec(3) pdec(3) replace
reg disconnections_ratex1000 $ratecontrols $fe housing_* if majority_minority==1
outreg2 using TableS5.rtf, keep($discontrols i.utility2 housing_*) stats(coef se) alpha(0.01, 0.05, 0.1) dec(3) pdec(3) append


***TABLE S6*****
reg disconnections black_percent hispanic_percent under5_percent over65_percent income2 renter_percent hh2 costs2 temp_lag temp_lag2 tmean_mean protected_days i.month i.year i.utility2 housing_*
outreg2 using TableS6.rtf, keep(black_percent hispanic_percent under5_percent over65_percent income2 renter_percent hh2 energy_burden2 costs2 temp_lag temp_lag2 tmean_mean protected_days i.utility2) stats(coef se) alpha(0.01, 0.05, 0.1) dec(3) pdec(3) replace
reg disconnections_ratex1000 black_percent hispanic_percent under5_percent over65_percent income2 renter_percent costs2 temp_lag temp_lag2 tmean_mean protected_days i.month i.year i.utility2 housing_*
outreg2 using TableS6.rtf, keep(black_percent hispanic_percent under5_percent over65_percent income2 renter_percent energy_burden2 costs2 temp_lag temp_lag2 tmean_mean protected_days i.utility2) stats(coef se) alpha(0.01, 0.05, 0.1) dec(3) pdec(3) append


***TABLE S7*****
reg disconnections black_percent hispanic_percent under5_percent over65_percent income2 renter_percent hh2 costs2 hdd protected_days i.month i.year i.utility2 housing_*
outreg2 using TableS7.rtf, keep(black_percent hispanic_percent under5_percent over65_percent income2 renter_percent hh2 energy_burden2 costs2 hdd protected_days i.utility2) stats(coef se) alpha(0.01, 0.05, 0.1) dec(3) pdec(3) replace
reg disconnections_ratex1000 black_percent hispanic_percent under5_percent over65_percent income2 renter_percent costs2 hdd protected_days i.month i.year i.utility2 housing_*
outreg2 using TableS7.rtf, keep(black_percent hispanic_percent under5_percent over65_percent income2 renter_percent energy_burden2 costs2 hdd protected_days i.utility2) stats(coef se) alpha(0.01, 0.05, 0.1) dec(3) pdec(3) append
reg disconnections black_percent hispanic_percent under5_percent over65_percent income2 renter_percent hh2 costs2 cdd protected_days i.month i.year i.utility2 housing_*
outreg2 using TableS7.rtf, keep(black_percent hispanic_percent under5_percent over65_percent income2 renter_percent hh2 energy_burden2 costs2 cdd protected_days i.utility2) stats(coef se) alpha(0.01, 0.05, 0.1) dec(3) pdec(3) append
reg disconnections_ratex1000 black_percent hispanic_percent under5_percent over65_percent income2 renter_percent costs2 cdd protected_days i.month i.year i.utility2 housing_*
outreg2 using TableS7.rtf, keep(black_percent hispanic_percent under5_percent over65_percent income2 renter_percent energy_burden2 costs2 cdd protected_days i.utility2) stats(coef se) alpha(0.01, 0.05, 0.1) dec(3) pdec(3) append